﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Text;
using System.Web.SessionState;

using GDModel.Model;
using BLL;

namespace Web.gate
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class battle1 : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //Session对单个浏览器记录上次时间，判断间隔
            if (HttpContext.Current.Session["LastVisitTime"] != null)
            {
                if (1 == 1)
                {
                    //如果小于访问时间间隔，视为作弊，1秒
                }
                else
                {
 
                }
            }
            else
            {
                HttpContext.Current.Session["LastVisitTime"] = DateTime.Now;
            }

            string action = context.Request["action"];
            string id = context.Request["id"];
            string _cha =context.Request["_cha"];
            string _mon = context.Request["_mon"];
            

            StringBuilder sb = new StringBuilder();

            Health health1 = new Health();//mon
            Health health2 = new Health();//cha

            switch (action)
            {
                case "attack":
                    {
                        Battle.OneInOne(Convert.ToInt32(_cha), Convert.ToInt32(_mon), out health1, out health2);

                        if (health1!=null && health2 !=null)
                        sb.Append("{Mon:{\"Health\":" + health1.Remain.ToString() + ",\"Hurt\":" + health1.Hurt.ToString()
                            + "},Cha:{\"Health\":" + health2.Remain.ToString() +",\"Hurt\":" + health2.Hurt.ToString() +"}}");
                        else
                            sb.Append("{Mon:{\"Health\":-1,\"Hurt\":-1},Cha:{\"Health\":-1,\"Hurt\":-1}}");
                        break;
                    }
                case "getitem":
                    {
                        Capture capture = null;
                        Battle.GetItem(Convert.ToInt32(_cha), Convert.ToInt32(_mon),out capture);
                        if (capture != null)
                        {
                            sb.Append("{Item:\"" + capture.IItem.Name + "\"}");
                        }
                        else
                        {
                            sb.Append("{Item:\"-1\"}");
                        }
                        break;
                    }
                default: break;
            }

            context.Response.Write(sb.ToString());

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }



 
}
